package school.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import school.domain.Elderly;
import school.extra.base.BaseController;
import school.extra.layui.ResponseList;
import school.service.ElderlyService;
import zhibi.frame.mybatis.example.Example;


@RequestMapping("elderly")
@Controller
public class ElderlyController extends BaseController {

    @Autowired
    private ElderlyService elderlyService;

    /**
     * 列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    public ResponseList list(@RequestParam(defaultValue = "") String word) {
        Example example = Example.getInstance()
                .addParam("username like '%" + word + "%'" +
                        " or phone like '%" + word + "%'" +
                        " or history like '%" + word + "%'" +
                        " or address like '%" + word + "%'");
        return new ResponseList(elderlyService.selectByExample(example));
    }

    @PostMapping("add")
    public String add(Elderly elderly) {
        session.setAttribute("tipMsg", "添加成功");
        elderlyService.insertSelective(elderly);
        return redirect("/index");
    }

    /**
     * 删除
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("del/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        elderlyService.deleteByPrimaryKey(id);
        return refresh();
    }


}
